﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KoPortal.System;
using MySql;

namespace MySQLConnVerify
{

    public partial class Form1 : Form
    {
        #region Members
        public DataTable tbl = new DataTable();
        DataTable tblSrv = new DataTable();
        bool isFinish = true;
        #endregion Members

        public Form1()
        {
            InitializeComponent();
        }

        private void btnMySQLVerifyAllConnections_Click(object sender, EventArgs e)
        {
            if (isFinish == false)
                return;
            isFinish = false;
            btnMySQLVerifyAllConnections.Enabled = false;
            string connStr = KoEnvironment.DBMySQLMainConnStr;
            string sql =
@"SELECT * FROM shops_sale WHERE (accept=1) ORDER BY name";
            tbl = new DataTable();
            tbl.Columns.Add("Srv IP");
            tbl.Columns.Add("connected");
            try
            {
                using (MySql.Data.MySqlClient.MySqlConnection conn =
                    new MySql.Data.MySqlClient.MySqlConnection(connStr))
                {
                    conn.Open();
                    using (MySql.Data.MySqlClient.MySqlCommand cmd =
                        new MySql.Data.MySqlClient.MySqlCommand(sql, conn))
                    {
                        cmd.CommandTimeout = 8000;
                        cmd.CommandType = global::System.Data.CommandType.Text;
                        using (MySql.Data.MySqlClient.MySqlDataAdapter a =
                            new MySql.Data.MySqlClient.MySqlDataAdapter(cmd))
                        {
                            tblSrv = new DataTable();
                            a.Fill(tblSrv);
                            dgMySQLServers.DataSource = tbl;
                            timer1.Enabled = true;                            
                        }
                    }
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private Exception VerifyMySqlIP(string parAddr, string parUserID, string parPwd, string parDBName)
        {
            Exception ret = null;
            string connStr = string.Format(
@"server={0};User Id={1};password={2};Persist Security Info=True;database={3};"
, parAddr, parUserID, parPwd, parDBName);
            try
            {
                using (MySql.Data.MySqlClient.MySqlConnection conn =
                    new MySql.Data.MySqlClient.MySqlConnection(connStr))
                {
                    conn.Open();
                    if (conn.State != ConnectionState.Closed)
                        conn.Close();
                }
            }
            catch (Exception ex)
            {
                ret = new Exception(ex.Message, ex);
            }
            return ret;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            foreach (DataRow row in tblSrv.Rows)
            {
                DataRow r = tbl.NewRow();
                Exception err = null;
                err = VerifyMySqlIP(
                      row["ip"].ToString()
                    , row["user"].ToString()
                    , row["pass"].ToString()
                    , row["db"].ToString());
                r[0] = row["ip"].ToString();
                if (err == null)
                    r[1] = "Yes";
                else
                    r[1] = "Err: " + err.Message;
                tbl.Rows.Add(r);
            }
            isFinish = true;
            btnMySQLVerifyAllConnections.Enabled = true;
        }
    }

    public class MyDataItems : Object
    {
        public string name = "name";
        public string value = "name";
    }

}
